#!/usr/bin/env ruby
# frozen_string_literal: true

require 'optparse'
require_relative '../lib/shell'
require_relative '../lib/packages'

repository = {
  local: false,
  target: 'https://github.com/rofl0r/proxychains-ng.git'
}
OptionParser.new do |opts|
  opts.banner = 'Installing proxychains-ng'

  opts.on('-d', '--dir=Dir', '使用目录而不是从 git 仓库获取路径') do |folder|
    repository[:local] = true
    repository[:target] = folder
  end

  opts.on('-r', '--repo=Url', '指定 Git 仓库地址') do |url|
    repository[:target] = url
  end
end.parse!

packages.install 'git', 'gcc', 'make'

if repository[:local]
  workdir = repository[:target]
else
  workdir = "/tmp/proxychains-ng-#{Time.now.to_i}"
  shell.exe "git clone #{repository[:target]} #{workdir}"
end

Dir.chdir(workdir) do
  shell.exe './configure --prefix=/usr --sysconfdir=/etc'
  shell.exe 'make'
  shell.exe 'sudo make install'
  shell.exe 'sudo make install-config'
end
